//
//  PauseLayer.cpp
//  NinjaTurtle
//
//  Created by Nguyen Tri Chung on 2/25/15.
//
//

#include "PauseLayer.h"

PauseLayer::PauseLayer(View* parent) {
    this->parent = parent;
    this->autorelease();
    this->initialize();
    this->parentButtons = new Vector<MenuItemSprite*>();
}

PauseLayer::~PauseLayer() {
    CC_SAFE_DELETE(this->parentButtons);
}

void PauseLayer::initialize() {
    this->createLayers();
    this->createLayersContent();
}

void PauseLayer::createLayers() {
    this->backgroundLayer = LayerColor::create(Color4B(0, 0, 0, 180));
    this->addChild(this->backgroundLayer);
    
    this->mainLayer = Layer::create();
    this->addChild(this->mainLayer);
    
    this->mainMenu = Menu::create();
    this->mainMenu->setPosition(Point::ZERO);
    this->mainLayer->addChild(this->mainMenu);
}

void PauseLayer::createLayersContent() {
    Size winSize = Director::getInstance()->getWinSize();
    
    // Background layer
    {
        FiniteTimeAction* fadeInAction = FadeTo::create(0.4f, 180);
        FiniteTimeAction* fadeOutAction = FadeOut::create(0.4f);
        __Dictionary* actionDict = __Dictionary::create();
        actionDict->setObject(fadeInAction, STATE_SHOW);
        actionDict->setObject(fadeOutAction, STATE_HIDE);
        this->backgroundLayer->setUserObject(actionDict);
    }
    
    // Dialog layer bg
    this->dialogFace = new CSprite();
    this->dialogFace->initWithSpriteFrameName(FRAME_PAUSE_DIALOG);
    this->dialogFace->setPosition(winSize.width / 2, winSize.height / 2);
    this->mainLayer->addChild(this->dialogFace);
    Size dialogBoardFaceSize = this->dialogFace->getContentSize();
    {
        FiniteTimeAction* scaleUp = ScaleTo::create(0.2f, 1.1f);
        FiniteTimeAction* scaleDown = ScaleTo::create(0.1f, 0.9f);
        FiniteTimeAction* scaleNormal = ScaleTo::create(0.1f, 1);
        FiniteTimeAction* showDialogAction = Sequence::create(scaleUp, scaleDown, scaleNormal, NULL);
        this->dialogFace->retainAction(showDialogAction, STATE_SHOW);
        
        FiniteTimeAction* scaleOut = ScaleTo::create(0.3f, 0);
        FiniteTimeAction* callbackFunction = CallFunc::create(CC_CALLBACK_0(PauseLayer::functionCallback, this, STATE_HIDE));
        
        FiniteTimeAction* hideDialogAction = Sequence::create(scaleOut, callbackFunction, NULL);
        this->dialogFace->retainAction(hideDialogAction, STATE_HIDE);
    }
    
    // Dialog menu
    Menu* dialogMenu = Menu::create();
    dialogMenu->setPosition(Point::ZERO);
    this->dialogFace->addChild(dialogMenu);
    
    this->continueButton = CNodeUtil::createButtonFontSmall(TXT_CONTINUE, 12, FRAME_BUTTON_SMALL, FRAME_BUTTON_SMALL_SELECTED);
    this->continueButton->setPosition(dialogBoardFaceSize.width / 2, dialogBoardFaceSize.height - 80);
    dialogMenu->addChild(this->continueButton);
    
    this->restartButton = CNodeUtil::createButtonFontSmall(TXT_RESTART, 12, FRAME_BUTTON_SMALL, FRAME_BUTTON_SMALL_SELECTED);
    this->restartButton->setPosition(dialogBoardFaceSize.width / 2, this->continueButton->getPosition().y - 55);
    dialogMenu->addChild(this->restartButton);
    
    this->quitButton = CNodeUtil::createButtonFontSmall(TXT_QUIT, 12, FRAME_BUTTON_SMALL, FRAME_BUTTON_SMALL_SELECTED);
    this->quitButton->setPosition(dialogBoardFaceSize.width / 2, this->restartButton->getPosition().y - 55);
    dialogMenu->addChild(this->quitButton);
}

void PauseLayer::functionCallback(const string& data) {
    if (data == STATE_HIDE) {
        this->setVisible(false);
        this->enableParentButton(true);
    }
}

void PauseLayer::show() {
    this->enableButton(true);
    this->enableParentButton(false);
    
    // background layer action
    __Dictionary* backgroundLayerActionDict = (__Dictionary*) this->backgroundLayer->getUserObject();
    Action* showBackgroundAction = (Action*) backgroundLayerActionDict->objectForKey(STATE_SHOW);
    Action* hideBackgroundAction = (Action*) backgroundLayerActionDict->objectForKey(STATE_HIDE);
    if (hideBackgroundAction != NULL) {
        this->backgroundLayer->getActionManager()->removeAction(hideBackgroundAction);
    }
    if (showBackgroundAction != NULL) {
        this->backgroundLayer->setOpacity(0);
        this->backgroundLayer->getActionManager()->removeAction(showBackgroundAction);
        this->backgroundLayer->runAction(showBackgroundAction);
    }
    
    // dialog action
    Action* showDialogAction = this->dialogFace->getActionExactly(STATE_SHOW);
    Action* hideDialogAction = this->dialogFace->getActionExactly(STATE_HIDE);
    if (hideDialogAction != NULL) {
        this->dialogFace->getActionManager()->removeAction(hideDialogAction);
    }
    if (showDialogAction != NULL) {
        this->setVisible(true);
        this->dialogFace->cocos2d::CCNode::setScale(0, 0);
        this->dialogFace->getActionManager()->removeAction(showDialogAction);
        this->dialogFace->runAction(showDialogAction);
    }
}

void PauseLayer::hide() {
    this->enableButton(false);
    
    __Dictionary* backgroundLayerActionDict = (__Dictionary*) this->backgroundLayer->getUserObject();
    Action* hideBackgroundAction = (Action*) backgroundLayerActionDict->objectForKey(STATE_HIDE);
    if (hideBackgroundAction != NULL) {
        this->backgroundLayer->getActionManager()->removeAction(hideBackgroundAction);
        this->backgroundLayer->runAction(hideBackgroundAction);
    }
    
    Action* hideDialogAction = this->dialogFace->getActionExactly(STATE_HIDE);
    if (hideDialogAction != NULL) {
        this->dialogFace->getActionManager()->removeAction(hideDialogAction);
        this->dialogFace->runAction(hideDialogAction);
    }
}


void PauseLayer::enableParentButton(bool enabled) {
    for (int i = 0; i < this->parentButtons->size(); i++) {
        this->parentButtons->at(i)->setEnabled(enabled);
    }
}

void PauseLayer::enableButton(bool enabled) {
    this->continueButton->setEnabled(enabled);
    this->restartButton->setEnabled(enabled);
    this->quitButton->setEnabled(enabled);
}